﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Areas.Admin.Controllers
{
    public class IntroduceController : Controller
    {
        private readonly Manager.BLL.Admin.M_IntroduceBll bll = new Manager.BLL.Admin.M_IntroduceBll();
        //
        // GET: /Admin/Introduce/
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.IntroduceSet, Ope = "view")]
        public ActionResult Index()
        {
            IList<Manager.Model.Admin.M_Introduce> models = bll.GetModelList("");
            Manager.Model.Admin.M_Introduce model=null;
            if (models != null)
            {
                if(models.Count>0)
                    model=models[0];
            }
            if (model != null)
            {
                return View(model);
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.IntroduceSet, Ope = "view")]
        public ActionResult Index(Manager.Model.Admin.M_Introduce model)
        {
            IList<Manager.Model.Admin.M_Introduce> models = bll.GetModelList("");
            Manager.Model.Admin.M_Introduce temp = null;
            if (models != null)
            {
                if (models.Count > 0)
                    temp = models[0];
            }
            if (temp != null)
            {
                try
                {
                    model.IntroduceID = temp.IntroduceID;
                    if (ModelState.IsValid)
                    {
                        bool r = bll.Update(model);
                        if (r)
                            return RedirectToAction("Index", "Introduce");
                        else
                            ModelState.AddModelError("", Resources.Global.msg_error);
                    }
                }
                catch
                {
                    ModelState.AddModelError("", Resources.Global.msg_error);
                }
                return View(model);
            }
            else
            {
                try
                {

                    if (ModelState.IsValid)
                    {
                        int r = bll.Add(model);
                        if (r>0)
                            return RedirectToAction("Index", "Introduce");
                        else
                            ModelState.AddModelError("", Resources.Global.msg_error);
                    }
                }
                catch
                {
                    ModelState.AddModelError("", Resources.Global.msg_error);
                }
                return View(model);
            }
        }
    }
}
